/*
 * Copyright (c) 2007 Aleksey Nikiforov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * * Neither the name of 'Aleksey Nikiforov' nor the names of other contributors 
 *   may be used to endorse or promote products derived from this software 
 *   without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.lex.input.effects;

import com.jme.math.FastMath;

public class MinMaxCurrent {
	private float min;
	private float max;
	private float range;
	
	private float current;
	private boolean loop;
	
	public MinMaxCurrent(float min, float max,
			float current, boolean loop)
	{
		this.min = min;
		this.max = max;
		this.loop = loop;
		range = max - min;
		
		this.current = 0;
		addToCurrent(current);
	}
	
	public float getCurrent() {
		return current;
	}
	
	public void setCurrent(float current) {
		this.current = current;
		checkCurrent();
	}
	
	/**
	 * @param val value to add
	 * @return false if the limit has been reached, true otherwise
	 */
	public boolean addToCurrent(float val) {
		current += val;
		return checkCurrent();
	}
	
	/**
	 * This method is only useful when dealing with a looped system. The method
	 * finds the smallest delta such that current + delta = value.
	 * Example: if you are dealing with degrees in range [0, 360] and you want
	 * to find the delta between 359 and 1, then set current to 359
	 * and the call this method with value = 1.
	 * 
	 * @param value must be in range [min, max], unpredictable output otherwise
	 * @return the smallest delta between current and value
	 */
	public float findDelta(float value) {
		float halfRange = range / 2;
		
		float oldCurrent = current;
		setCurrent(value);
		float delta = current - oldCurrent;
		
		if (delta > halfRange) delta = delta - range;
		else if (delta < -halfRange) delta = range + delta;
		
		current = oldCurrent;
		return delta;
	}
	
	/**
	 * The passed value is not verified against the range [min, max].
	 * 
	 * @see org.lex.input.effects.MinMaxCurrent#findDelta(float)
	 */
	public float findDeltaNoVerify(float value) {
		float halfRange = range / 2;
		float delta = value - current;
		
		if (delta > halfRange) delta = delta - range;
		else if (delta < -halfRange) delta = range + delta;
		
		return delta;
	}
	
	private boolean checkCurrent() {
		if (loop) {
			
			if (current >= max) {
				current -= FastMath.floor((current-min)/range) * range;
			}
			if (current < min) {
				current += FastMath.ceil((min-current)/range) * range;
			}
				
		} else {
			
			if (current > max) {
				current = max;
				return false;
			}
			if (current < min){
				current = min;
				return false;
			}
			
		}
		
		return true;
	}

	public boolean isLoop() {
		return loop;
	}

	public void setLoop(boolean loop) {
		this.loop = loop;
	}

	public float getMax() {
		return max;
	}

	public void setMax(float max) {
		this.max = max;
		range = max - min;
	}

	public float getMin() {
		return min;
	}

	public void setMin(float min) {
		this.min = min;
		range = max - min;
	}
	
}
